package edu.buaa.act.travel.m.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import edu.buaa.act.travel.m.Constants;
import edu.buaa.act.travel.m.data.Position;
import edu.buaa.act.travel.m.data.SurroundItem;
import edu.buaa.act.travel.m.utils.JSONParser;

public class SurroundsModel {
	
	private List<SurroundItem> mSurroundsList;

	public SurroundsModel(final String result) {
		
		mSurroundsList = new ArrayList<SurroundItem>();

		final JSONObject _jo = JSONParser.getJSONObject(result);
		final JSONObject _tripJson = JSONParser.getJSONObject(_jo, Constants.KEY_SURROUNDS);
		final JSONArray _items = JSONParser.getJSONArray(_tripJson, Constants.KEY_RESOURCES);

		for (int i = 0; i < _items.length(); i++) {
			final JSONObject _item = JSONParser.getJSONObject(_items, i);
			
				final String name = JSONParser.getString(_item, Constants.KEY_NAME);
				final String address = JSONParser.getString(_item, Constants.KEY_ADDRESS);
				final int rank = JSONParser.getInt(_item, Constants.KEY_RANK);
				final double averageCost = JSONParser.getDouble(_item, Constants.KEY_AVERAGE_COST);
				final Double latDouble = JSONParser.getDouble(_item, Constants.KEY_LATITUDE);
				final Double lngDouble = JSONParser.getDouble(_item, Constants.KEY_LONGITUDE);
				SurroundItem _surroundItem=new SurroundItem(name,address,rank,averageCost,new Position(latDouble, lngDouble));		
				mSurroundsList.add(_surroundItem);
				Log.v("UI", name);
		}
	}

	public List<SurroundItem> getSurroundsList() {
		return mSurroundsList;
	}

}
